﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SQLite;
using System.IO;

namespace PathSearcher
{
    class Database
    {
        private static SQLiteConnection connection;
        private static String databaseFile = "database.db";

        public static SQLiteConnection GetConnection()
        {
            if (connection != null)
                return connection;

            bool emptyDatabase = false;

            //File.Delete(databaseFile);


            DirectoryInfo rootDir = new DirectoryInfo(@"\");
            foreach (DirectoryInfo di in rootDir.GetDirectories())
            {
                //if directory and temporary
                if ((di.Attributes == (FileAttributes.Directory | FileAttributes.Temporary)))
                {
                    //add to collection of storage cards
                    databaseFile = di.Name + "\\" + databaseFile;
                    break;
                }
            }


            if (File.Exists(databaseFile) == false)
            {
                emptyDatabase = true;
            }

            if (connection == null || (connection.State != System.Data.ConnectionState.Open
                || connection.State != System.Data.ConnectionState.Connecting))
            {
                connection = new SQLiteConnection("Data Source="+databaseFile+";");
                connection.Open();

                //enable foreign key support
                SQLiteCommand pragmaFkCommand = new SQLiteCommand(connection);
                pragmaFkCommand.CommandText = "PRAGMA foreign_keys;";
                pragmaFkCommand.ExecuteNonQuery();

                /*
                //create table structure if necessary 
                SQLiteCommand databaseStructureCmnd = new SQLiteCommand(connection);
                databaseStructureCmnd.CommandText = Resources.sqlscript;
                databaseStructureCmnd.ExecuteNonQuery();

                //insert default data if necessary
                if (emptyDatabase)
                {
                    SQLiteCommand defaultDataCmnd = new SQLiteCommand(connection);
                    defaultDataCmnd.CommandText = Resources.default_data;
                    defaultDataCmnd.ExecuteNonQuery();
                }
                */
                /*
                SQLiteCommand cmnd = new SQLiteCommand("attach database ':memory:' as mem;", connection);
                cmnd.ExecuteNonQuery();
                SQLiteCommand loadMemory = new SQLiteCommand("create table mem.time_table as select * from time_table",connection);
                loadMemory.ExecuteNonQuery();
                */
            }
            return connection;
        }

        ~Database()
        {
            if (connection != null && connection.State != System.Data.ConnectionState.Closed)
            {
                connection.Close();
            }
        }
    }
}
